<template>
  <div>
    <el-dialog :title="modalObj.title" :visible="modalObj.show" :before-close="handleCancel" center width="500px">
      <el-form ref="editFormRef" :model="editForm" :rules="rules" size="small">
        <el-form-item label="设备标签类型" :label-width="labelWidth" prop="labelTypeId">
          <el-select v-model="editForm.labelTypeId" clearable placeholder="请选择标签类型">
            <el-option
              v-for="item in eqptTagTypeList"
              :key="item.labelTypeId"
              :label="item.labelTypeName"
              :value="item.labelTypeId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="设备标签编号" :label-width="labelWidth" prop="labelNumber">
          <el-input v-model="editForm.labelNumber" placeholder="请输入设备标签编号"></el-input>
        </el-form-item>

        <el-form-item label="标签名称" :label-width="labelWidth" prop="labelName">
          <el-input placeholder="请输入标签名称" v-model="editForm.labelName"></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="handleCancel">取 消</el-button>
        <el-button type="primary" @click="handleOk('editFormRef')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addDeviceLabel } from '@/views/children/safety/js/device'
import { Message } from 'element-ui'
import { validateStrLenNo } from '@/utils/validate'
export default {
  name: 'addDeviceTag',
  components: {},
  props: {
    modalObj: {
      type: Object,
      default: () => ({})
    },
    eqptTagTypeList: {
      type: Array,
      default: () => ([])
    }
  },
  data () {
    return {
      labelWidth: '120px',
      editForm: {
        labelName: '', // 设备标签名称
        labelNumber: '', // 设备标签编号
        labelTypeId: ''
      },
      rules: {
        labelTypeId: [{ required: true, message: '请选择标签类型', trigger: 'change' }],
        labelNumber: [
          { required: true, message: '请输入设备标签编号', trigger: 'blur' },
          { validator: validateStrLenNo, trigger: 'blur' }
        ],
        labelName: [{ required: true, message: '请输入设备标签名称', trigger: 'blur' }]
      }
    }
  },
  methods: {
    // 提交表单
    handleOk (editFormRef) {
      this.$refs[editFormRef].validate(async valid => {
        if (valid) {
          let res
          let query = {
            ...this.editForm
          }
          delete query.createTime
          res = await addDeviceLabel(query)
          if (res.code === 0) {
            this.$emit('ok')
            this.$message.success('保存成功')
            this.handleCancel()
          } else {
            Message({
              type: 'error',
              message: res.msg
            })
          }
        }
      })
    },
    // 取消保存
    handleCancel () {
      this.$emit('close')
    }
  },
  mounted () {
    this.editForm = { ...this.editForm, ...this.modalObj.row }
  }
}
</script>
<style scoped lang="scss">
::v-deep .el-form-item__content, .el-select {
  width: 260px;
}
</style>
